<!---
Copyright (c) 2007 Brian Billman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
--->
<cfcomponent displayname="CFSmugSubCategories" output="false" extends="CFSmugBase">

	<cffunction name="get" access="public" output="false" returntype="array" hint="This method returns a list of SubCategories for the specified user (identified by SessionID or (optional) NickName) in the specified Category (identified by CategoryID).">
		<cfargument name="SessionID" type="string" required="true" />
		<cfargument name="CategoryID" type="numeric" required="true" />
		<cfargument name="NickName" type="string" required="false" hint="Required if an anonymous SessionId" />
		<cfargument name="SitePassword" type="string" required="false" />

		<cfset var response = 0 />
		<cfset var argsCopy = structNew() />
		<cfset var result = 0 />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.get ), argsPassed = Arguments ) />

		<cfset response = doRemoteInvocation( method = "smugmug.subcategories.get",
											  args = argsCopy,
										  	  secureMode = StructKeyExists( Arguments, "SitePassword" ) )  />

		<cftry>
			<cfset result = convertResponse( response ) />

			<cfcatch type="CFSmug.SmugMugException">
				<!--- if "empty set - no subcategories found", return empty array --->
				<cfif cfcatch.errorcode eq getErrorCodes().EMPTY_SET>
					<cfset result = ArrayNew( 1 ) />
				<cfelse>
					<cfrethrow />
				</cfif>
			</cfcatch>
		</cftry>

		<cfreturn result />
	</cffunction>

	<cffunction name="getAll" access="public" output="false" returntype="array" hint="This method returns a list of SubCategories for the specified user (identified by SessionID or (optional) NickName). Unlike smugmug.subcategories.get, this returns a complete list, rather than one just for a given Category.">
		<cfargument name="SessionID" type="string" required="true" />
		<cfargument name="NickName" type="string" required="false" hint="Required if an anonymous SessionId" />
		<cfargument name="SitePassword" type="string" required="false" />

		<cfset var response = 0 />
		<cfset var argsCopy = structNew() />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.getAll ), argsPassed = Arguments ) />

		<cfset response = doRemoteInvocation( method = "smugmug.subcategories.getAll",
											  args = argsCopy,
										  	  secureMode = StructKeyExists( Arguments, "SitePassword" ) )  />


		<cfreturn convertResponse( response ) />
	</cffunction>

	<cffunction name="create" access="public" output="false" returntype="numeric" hint="This method creates a new SubCategory with the given Name. Returns the new SubCategoryID.">
		<cfargument name="SessionID" type="string" required="true" />
		<cfargument name="Name" type="string" required="true" hint="Category Name" />
		<cfargument name="CategoryID" type="numeric" required="true" />

		<cfset var response = 0 />
		<cfset var argsCopy = structNew() />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.create ), argsPassed = Arguments ) />

		<cfset response = doRemoteInvocation( method = "smugmug.subcategories.create",
											  args = argsCopy )  />

		<cfreturn convertResponse( response ) />
	</cffunction>

	<cffunction name="delete" access="public" output="false" returntype="void" hint="This method deletes the specified SubCategory.">
		<cfargument name="SessionID" type="string" required="true" />
		<cfargument name="SubCategoryID" type="numeric" required="true" />

		<cfset var response = 0 />
		<cfset var argsCopy = structNew() />
		<cfset var result = 0 />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.delete ), argsPassed = Arguments ) />

		<cfset response = doRemoteInvocation( method = "smugmug.subcategories.delete",
											  args = argsCopy )  />

		<cfset result = convertResponse( response ) />

		<cfreturn />
	</cffunction>

	<cffunction name="rename" access="public" output="false" returntype="void" hint="This method renames the specified SubCategory with new Name.">
		<cfargument name="SessionID" type="string" required="true" />
		<cfargument name="SubCategoryID" type="numeric" required="true" />
		<cfargument name="Name" type="string" required="true" hint="New Category Name" />

		<cfset var response = 0 />
		<cfset var argsCopy = structNew() />
		<cfset var result = 0 />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.rename ), argsPassed = Arguments ) />

		<cfset response = doRemoteInvocation( method = "smugmug.subcategories.rename",
											  args = argsCopy )  />

		<cfset result = convertResponse( response ) />

		<cfreturn />
	</cffunction>

	<cffunction name="convertResponse" access="private" output="false" returntype="any">
		<cfargument name="responseXML" type="xml" required="true" />
		<cfset var x = 0 />
		<cfset var s = 0 />
		<cfset var c1 = 0 />

		<cftry>

			<cfif validateRESTResponse( Arguments.responseXML )>
				<cfset x = Arguments.responseXML.rsp />
			</cfif>

			<cfswitch expression="#LCase( x.method.XmlText )#">

				<cfcase value="smugmug.subcategories.get">
					<!---
						Array of structs "Categories":
							int "id"
							String "Title"
					--->

					<cfset s = ArrayNew(1) />
					<cfloop from="1" to="#ArrayLen( x.SubCategories.SubCategory )#" index="c1">
						<cfset s[c1] = StructNew() />
						<cfset s[c1] = createSubCategoryStruct( x.SubCategories.SubCategory[c1] ) />
					</cfloop>
				</cfcase>

				<cfcase value="smugmug.subcategories.getall">
					<!---
						Array of structs: [empty array if no subcategories]
							int "CategoryID"
							int "SubCategoryID"
							String "Title"
					--->

					<cfset s = ArrayNew(1) />
					<cfloop from="1" to="#ArrayLen( x.SubCategories.SubCategory )#" index="c1">
						<cfset s[c1] = StructNew() />
						<cfset s[c1] = createSubCategoryStruct( x.SubCategories.SubCategory[c1] ) />
					</cfloop>
				</cfcase>

				<cfcase value="smugmug.subcategories.create">
					<!---
						int "CategoryID"
					--->
					<cfset s = x.SubCategory.XmlAttributes.id />
				</cfcase>

				<cfcase value="smugmug.subcategories.delete;smugmug.subcategories.rename" delimiters=";">
					<!---
						String == "SUCCESS"
					--->
					<cfset s = "SUCCESS" />
				</cfcase>

				<cfdefaultcase>
					<cfset logAction( file="CFSmug.CFSmugSubCategories.convertResponse", text="Message = Received response from unexpected method: '#x.method.XmlText#'", type="Warning" ) />
					<cfthrow type="CFSmug.CFSmugSubCategories.convertResponse" message="Received response from unexpected method: '#x.method.XmlText#'" />
				</cfdefaultcase>

			</cfswitch>

			<cfreturn s />


			<cfcatch type="CFSmug">
				<cfrethrow />
			</cfcatch>
			<cfcatch type="any">
				<cfset logAction( file="CFSmug.CFSmugSubCategories.convertResponse", text="Error converting response; Message=#cfcatch.message#; Detail=#cfcatch.detail#; Stacktrace=#cfcatch.stacktrace#", type="Error" ) />
				<cfthrow type="CFSmug.CFSmugSubCategories.convertResponse"
						message="Error converting response" detail="Message: #cfcatch.message#; Detail: #cfcatch.detail#" />
			</cfcatch>
		</cftry>
	</cffunction>

</cfcomponent>